package com.k2data.k2app.controller;

import com.github.pagehelper.PageHelper;
import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.domain.po.Problem;
import com.k2data.k2app.domain.po.v1.Fault;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.PageInfo;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.ProblemService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by k2data on 18-1-18.
 */
@Controller
@RequestMapping("/problem")
public class ProblemController {

    private final ProblemService problemService;

    @Autowired
    public ProblemController(ProblemService problemService){
        this.problemService=problemService;
    }

    @ApiOperation(value = "获取诊断信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path")
    })
    @GetMapping(value = "/{id}")
    public ResponseEntity<CommonResultResponse> getProblem(@PathVariable("id") Long id)throws Exception{
        return ResponseHelper.success(problemService.getProblemById(id));
    }

    @ApiOperation(value = "诊断查询条件列表")
    @ApiImplicitParams({
    })
    @GetMapping(value="/list")
    public ResponseEntity<CommonResultResponse> getSelectList()throws Exception{
        return ResponseHelper.success(problemService.getSelectList());
    }

    @ApiOperation(value = "条件查询诊断信息列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页大小(默认 30)", dataType = "int", paramType = "query"),
        @ApiImplicitParam(name = "vibrationDominantFrequency", value = "振动主导频率", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "vibrationDirection", value = "振动方向", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "withFrequency", value = "常伴频率", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "vibrationStability", value = "振动稳定性", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "phaseCharacteristics", value = "相位特征", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "vibrationLoadChange", value = "振动随负荷变化", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "vibrationSpeedChange", value = "振动随速度变化", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "vibrationOilChange", value = "振动随油温变化", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "vibrationPressureChange", value = "振动随出口压力变化", dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "rotorTrajectory", value = "转子轨迹", dataType = "String", paramType = "query")
    })
    @GetMapping(value="/problems")
    public ResponseEntity<CommonResultResponse> getProblemList(
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "vibrationDominantFrequency", required = false) String vibrationDominantFrequency,
            @RequestParam(value = "vibrationDirection", required = false) String vibrationDirection,
            @RequestParam(value = "withFrequency", required = false) String withFrequency,
            @RequestParam(value = "vibrationStability", required = false) String vibrationStability,
            @RequestParam(value = "phaseCharacteristics", required = false) String phaseCharacteristics,
            @RequestParam(value = "vibrationLoadChange", required = false) String vibrationLoadChange,
            @RequestParam(value = "vibrationSpeedChange", required = false) String vibrationSpeedChange,
            @RequestParam(value = "vibrationOilChange", required = false) String vibrationOilChange,
            @RequestParam(value = "vibrationPressureChange", required = false) String vibrationPressureChange,
            @RequestParam(value = "rotorTrajectory", required = false) String rotorTrajectory
    )throws Exception{

        Problem problem =new Problem();
        problem.setVibrationDominantFrequency(vibrationDominantFrequency);
        problem.setVibrationDirection(vibrationDirection);
        problem.setWithFrequency(withFrequency);
        problem.setVibrationStability(vibrationStability);
        problem.setPhaseCharacteristics(phaseCharacteristics);
        problem.setVibrationLoadChange(vibrationLoadChange);
        problem.setVibrationSpeedChange(vibrationSpeedChange);
        problem.setVibrationOilChange(vibrationOilChange);
        problem.setVibrationPressureChange(vibrationPressureChange);
        problem.setRotorTrajectory(rotorTrajectory);
        List<Problem> list= problemService.getProblems(problem);

        pageSize = (pageSize == null) ? Integer.valueOf(30) : pageSize;
        pageNum = (pageNum == null) ? Integer.valueOf(1) : pageNum;
        PageHelper.startPage(pageNum, pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize);
        PageInfo pageInfo = new PageInfo(pageNum, pageSize, list.size());

        int fromIndex = (pageNum - 1) * pageSize;
        if (fromIndex > list.size()) {
            fromIndex = list.size();
        }
        int toIndex = pageNum * pageSize;
        if (toIndex > list.size()) {
            toIndex = list.size();
        }

        return ResponseHelper.success(list.subList(fromIndex,toIndex),pageInfo);
    }

    @ApiOperation(value = "添加诊断信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "problem", value = "problem", required = true, dataType = "Problem", paramType = "body")
    })
    @PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> addProblem(@RequestBody Problem problem)throws Exception{
        return ResponseHelper.success(problemService.addProblem(problem));
    }

    @ApiOperation(value = "删除诊断信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
    })
    @DeleteMapping(value = "/{id}")
    public ResponseEntity<CommonResultResponse> delProblem(@PathVariable("id") Long id)throws Exception{
        return ResponseHelper.success(problemService.delProblem(id));
    }

    @ApiOperation(value = "修改诊断信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "problem", value = "problem", required = true, dataType = "Problem", paramType = "body")
    })
    @PutMapping(value = "/{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<CommonResultResponse> putProblem(@PathVariable("id") Long id,
                                                         @RequestBody Problem problem) throws Exception{
        problem.setId(id);
        return ResponseHelper.success(problemService.updateProblem(problem));
    }
}
